<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        let fang = new FangPL()
            // 记录房源类型
        let houseType = urlSearchs()["housetype"] ? urlSearchs()["housetype"] : 1 // 1-代表新房 ，2 -  代表二手房
        let housePurpose = urlSearchs()["housetype"] ? urlSearchs()["housetype"] : 1 //  1: 住宅 2：公寓 3：厂房 4：商铺 5：写字楼
            // 声明数组保存搜索数据 区分 新房 二手房
        let newHousesList = []
        let secondHousesList = []
            // 声明变量保存搜索框内容
        let searchVal = ""
            // 搜索结果函数 - 新房
        const searchResult = (result, sName) => {
                var html = ''
                if (isArray(result) && result.length) {
                    result.forEach((item, idx) => {
                        html += `<div class="searchCell" data-hidx="${idx}">              
                        <div class="searchResult">
                            <i></i>
                            <div class="right">
                                <p>
                                    <span class="houseName">${item?.hName ? item.hName : ""}</span>
                                    <span class="houseType">住房</span>
                                </p>
                                <p class="houseAddr">${item.hProvince ? item.hProvince : ""}-${item.hCity ? item.hCity : ""}-${item.hDistrict ? item.hDistrict : ""}</p>
                            </div>
                        </div>
                        <span class="distance">6.2km</span>
                    </div>`
                    })
                }
                return html
            }
            // 获取搜索内容 - 新房
        const getSearchVal = function(e) {
            let sData = {
                    hName: e.value.trim(),
                    PageIndex: 1,
                    lhousType: 1,
                    Token: getToken(),
                    IType: 1,
                    UserType: 1
                }
                // 获取搜索内容区父元素
            let searchContent = document.querySelector(".searchContent")
                //发请求 -- 搜索
            fang.axios.post("/api/public/SelecthName", sData)
                .then(({
                    rCode,
                    rData
                }) => {
                    if (rCode == "0000") {
                        newHousesList = rData.clsHouseInfos // 暂存数据
                        console.log(rData)
                        searchContent.innerHTML = searchResult(newHousesList, sData.Name)
                    } else if (rCode == "0004") {
                        // 身份验证失败

                        renderLogin()
                    } else {
                        searchContent.innerHTML = ""
                    }
                })
        }

        // 监听输入事件
        const inputSearch = (inputDom) => {
            var doing = false;
            //
            // 获取输入框元素并添加监听事件
            let searchInput = document.querySelector(inputDom)
                // 搜索事件
            searchInput.addEventListener('compositionstart', function(e) {
                doing = true;
            }, false);
            searchInput.addEventListener('input', function(e) {
                if (!doing) {
                    searchVal = e.target.value //  保存搜索内容
                    getSearchVal(e.target);
                }
            }, false);
            searchInput.addEventListener('compositionend', function(e) {
                doing = false;
                searchVal = e.target.value //  保存搜索内容
                getSearchVal(e.target);
            })
        }

        // 弹出选择器 市区
        let isClick = false
        let searchHouse = this.document.getElementsByClassName("ancestor")[0]
        searchHouse.addEventListener("touchstart", function(e) {
            isClick = true
        })
        searchHouse.addEventListener("touchmove", function(e) {
            isClick = false
        })
        searchHouse.addEventListener("touchend", function(e) {
            if (!isClick) {
                return
            }
            let that = e.target
            if (isParent(that, "searchItem")) {
                // 点击搜索结果栏 跳转到楼盘详情页
                var hindex = getParent(that, "searchItem").dataset.hindex
                window.location.href = `/web/buy/housing/newhouse/uDetails.html?hindex=${hindex}`
            } else if (that.className == "back") {
                // 点击返回
                window.history.go(-1)

            } else if (that.className == "confirm") {
                // 确定
                setHousingResource("houseName", document.querySelector("#search").value)
                setLocalStorage("houseName", document.querySelector("#search").value, "houseNameInfo")
                window.history.go(-1)
            } else if (isParent(that, "searchCell")) {

                setHousingResource("houseName", getChildEL(getParent(that, "searchCell"), "houseName").innerHTML)
                setLocalStorage("houseName", getChildEL(getParent(that, "searchCell"), "houseName").innerHTML, "houseNameInfo")
                window.history.go(-1)
            }
        });
        /***************************模糊搜索*********************** */
        inputSearch("#search")
    </script>
</body>

</html>